﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml.Linq;

namespace ZhangZhao.RenChangYou
{
    public class GEO
    {
        public static readonly string apiKey = "09ef1cb60804669eadb0e1aedebc627c9e3cac21699d230b2429a3c912052955";
        

        public LocationInfo GetLocation()
        {
            string ip = GetIP();
            string url = "http://api.ipinfodb.com/v2/ip_query.php?key={0}&ip={1}&timezone=true";

            url = String.Format(url, apiKey, ip);

            try
            {
                HttpWebRequest httpWRequest = (HttpWebRequest)WebRequest.Create(url);
                using (HttpWebResponse httpWResponse = (HttpWebResponse)httpWRequest.GetResponse())
                {
                    var result = XDocument.Load(httpWResponse.GetResponseStream());

                    var location = (from x in result.Descendants("Response")
                                    select new LocationInfo
                                    {
                                        CountryCode = (string)x.Element("CountryCode"),
                                        CountryName = (string)x.Element("CountryName"),
                                        RegionName = (string)x.Element("RegionName"),
                                        CityName = (string)x.Element("City"),
                                        ZipPostalCode = (string)x.Element("ZipPostalCode"),
                                        Timezone = new TimezoneInfo
                                        {
                                            TimezoneName = (string)x.Element("TimezoneName"),
                                            Gmtoffset = (float)x.Element("Gmtoffset"),
                                            Dstoffset = (bool)x.Element("Dstoffset")
                                        },
                                        Position = new LatLongInfo
                                        {
                                            Latitude = (float)x.Element("Latitude"),
                                            Longitude = (float)x.Element("Longitude")
                                        }
                                    }).FirstOrDefault();

                    return location;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string GetIP()
        {
            string ipAddress = string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"])
                                ? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
                                : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            return ipAddress;
        }
    }

    public class LocationInfo
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
        public string RegionName { get; set; }
        public string CityName { get; set; }
        public string ZipPostalCode { get; set; }
        public TimezoneInfo Timezone { get; set; }
        public LatLongInfo Position { get; set; }
    }

    public class TimezoneInfo
    {
        public string TimezoneName { get; set; }
        public float Gmtoffset { get; set; }
        public bool Dstoffset { get; set; }
    }

    public class LatLongInfo
    {
        public float Latitude { get; set; }
        public float Longitude { get; set; }
    }  
}